import path from 'path'
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

// 处理重复点击同一个路由报错的问题
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import componentsRouter from './modules/components'
import chartsRouter from './modules/charts'
import tableRouter from './modules/table'
import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */
// 菜单不显示，不要权限
export const routesC0Q0 = [
];
// 菜单不显示，要权限
export const routesC0Q1 = [
];
// 菜单不显示，一般不要权限
export const routesC0 = [
  {
    path: '/',
    component: Layout,
    redirect: '/download',
    children: [
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true // 在这里是默认，但是不能省
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
];
// 菜单显示，不要权限
export const routesC1Q0 = [
];
// 菜单显示，要权限
export const routesC1Q1 = [
];
// 菜单显示，一般要权限，也可以设置不要权限
export const routesC1 = [
  {
    name: 'redirect',
    children: [
      {
        name: 'redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      },
      {
        name: 'auth-redirect',
        component: () => import('@/views/login/auth-redirect'),
      },
    ]
  },
  {
    name: 'dashboard',
    component: () => import('@/views/dashboard/index'),
    meta: { title: '首页', icon: 'dashboard', affix: true, }
  },
  {
    name: 'download',
    component: () => import('@/views/documentation/index'),
    meta: { title: '下载', icon: 'clipboard', affix: true, }, // title 菜单显示标题，icon 菜单显示图标
  },
  {
    name: 'documentation',
    component: () => import('@/views/documentation/index'),
    meta: { title: 'Documentation', icon: 'documentation', affix: true }
  },
  {
    name: 'guide',
    component: () => import('@/views/guide/index'),
    meta: { title: 'Guide', icon: 'guide', noCache: true }
  },
  {
    name: 'profile',
    component: () => import('@/views/profile/index'),
    meta: { title: 'Profile', icon: 'user', noCache: true }
  },
  {
    name: 'permission',
    meta: {
      title: 'Permission',
      icon: 'lock',
      roles: ['admin', 'editor'] // you can set roles in root nav
    },
    children: [
      {
        name: 'page',
        component: () => import('@/views/permission/page'),
        meta: {
          title: 'Page Permission',
          roles: ['admin'] // or you can only set roles in sub nav
        }
      },
      {
        name: 'directive',
        component: () => import('@/views/permission/directive'),
        meta: {
          title: 'Directive Permission'
          // if do not set roles, means: this page does not require permission
        }
      },
      {
        name: 'role',
        component: () => import('@/views/permission/role'),
        meta: {
          title: 'Role Permission',
          roles: ['admin']
        }
      }
    ]
  },
  {
    name: 'icon',
    component: () => import('@/views/icons/index'),
    meta: { title: 'Icons', icon: 'icon', noCache: true }
  },

  /** when your routing map is too long, you can split it into small modules **/
  componentsRouter,
  chartsRouter,
  nestedRouter,
  tableRouter,

  {
    name: 'example',
    meta: {
      title: 'Example',
      icon: 'el-icon-s-help'
    },
    children: [
      {
        name: 'create',
        component: () => import('@/views/example/create'),
        meta: { title: 'Create Article', icon: 'edit' }
      },
      {
        name: 'edit/:id(\\d+)',
        component: () => import('@/views/example/edit'),
        meta: { title: 'Edit Article', noCache: true, activeMenu: '/example/list' },
        hidden: true
      },
      {
        name: 'list',
        component: () => import('@/views/example/list'),
        meta: { title: 'Article List', icon: 'list' }
      }
    ]
  },

  {
    name: 'tab',
    component: () => import('@/views/tab/index'),
    meta: { title: 'Tab', icon: 'tab' }
  },

  {
    name: 'error',
    meta: {
      title: 'Error Pages',
      icon: '404'
    },
    children: [
      {
        name: '401',
        component: () => import('@/views/error-page/401'),
        meta: { title: '401', noCache: true }
      },
      {
        name: '404',
        component: () => import('@/views/error-page/404'),
        meta: { title: '404', noCache: true }
      }
    ]
  },

  {
    name: 'error-log',
    component: () => import('@/views/error-log/index'),
    meta: { title: 'Error Log', icon: 'bug' }
  },

  {
    name: 'excel',
    meta: {
      title: 'Excel',
      icon: 'excel'
    },
    children: [
      {
        name: 'export-excel',
        component: () => import('@/views/excel/export-excel'),
        meta: { title: 'Export Excel' }
      },
      {
        name: 'export-selected-excel',
        component: () => import('@/views/excel/select-excel'),
        meta: { title: 'Export Selected' }
      },
      {
        name: 'export-merge-header',
        component: () => import('@/views/excel/merge-header'),
        meta: { title: 'Merge Header' }
      },
      {
        name: 'upload-excel',
        component: () => import('@/views/excel/upload-excel'),
        meta: { title: 'Upload Excel' }
      }
    ]
  },

  {
    name: 'zipDownload',
    component: () => import('@/views/zip/index'),
    name: 'ExportZip',
    meta: { title: 'Export Zip' }
  },

  // {
  //   path: '/pdf',
  //   component: Layout,
  //   redirect: '/pdf/index',
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/pdf/index'),
  //       name: 'PDF',
  //       meta: { title: 'PDF', icon: 'pdf' }
  //     }
  //   ]
  // },
  // {
  //   path: '/pdf/download',
  //   component: () => import('@/views/pdf/download'),
  //   hidden: true
  // },

  // {
  //   path: '/theme',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/theme/index'),
  //       name: 'Theme',
  //       meta: { title: 'Theme', icon: 'theme' }
  //     }
  //   ]
  // },

  // {
  //   path: '/clipboard',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/clipboard/index'),
  //       name: 'ClipboardDemo',
  //       meta: { title: 'Clipboard', icon: 'clipboard' }
  //     }
  //   ]
  // },

  // {
  //   path: 'external-link',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'https://github.com/PanJiaChen/vue-element-admin',
  //       meta: { title: 'External Link', icon: 'link' }
  //     }
  //   ]
  // },
];

export function menuRouterroutesF(routes, res, roles) {
  routes.forEach(route => {
  })
}
export function menuRouterroutesT(routesPath, routes, accessRoutes) {
  if (routes.children && routes.children.length > 0) {
    let flage = false;
    routes.children.forEach(route => {
      if (menuRouterroutesT(path.resolve(routesPath, routes.name), route, accessRoutes) == true) {
        flage = true;
      }
    })
    if (flage) {
      routes.hidden = false
      return true
    } else {
      routes.hidden = true
      return false
    }
  } else {
    if (routes.hidden != null && routes.hidden) {
      return false
    } else {
      routes.path = path.resolve(routesPath, routes.name)
      console.log("routes=", routes)
      accessRoutes.push(routes)
      return true
    }
  }
}
export function menuRouterS(permission, roles) {
  if (permission) {
    routesC1.forEach(route => {
      menuRouterroutesT('', route, routesC0[0].children)
    })
  } else {

  }
  return routesC1
}

const packageName = require("../../package.json").name;
const createRouter = () => {
  menuRouterS(true, [])
  return new Router({
    base: window.__POWERED_BY_QIANKUN__ ? `${packageName}` : "/",
    mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: routesC0
  })
}

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
